package com.aizhixin.lab.project.template.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.project.template.domain.WorkOrderFileDomain;
import com.aizhixin.lab.project.template.entity.Project;
import com.aizhixin.lab.project.template.entity.TaskWorkOrder;
import com.aizhixin.lab.project.template.entity.WorkOrderFile;
import com.aizhixin.lab.project.template.repository.TaskWorkOrderFileRepository;
import com.aizhixin.lab.project.template.repository.TaskWorkOrderRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WorkOrderFileService {
    @Autowired
    private TaskWorkOrderFileRepository workOrderFileRepository;

    public List<WorkOrderFile> save(List<WorkOrderFile> workOrderFileList) {
      return workOrderFileRepository.save(workOrderFileList);

    }

    public WorkOrderFile save(WorkOrderFile workOrderFile) {
      return  workOrderFileRepository.save(workOrderFile);
    }

    public List<WorkOrderFile> findWorkOrderFiles(String id) {
        return workOrderFileRepository.findByWorkOrderIdAndDeleteFlag(id, DataValidity.VALID.getState());
    }

    public Integer deleteByWorkOrderId(String workOrderId) {
        return workOrderFileRepository.deleteByWorkOrderId(workOrderId);
    }

    public Map<String, Object> delete(String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        WorkOrderFile workOrderFile = workOrderFileRepository.findOne(id);
        if (!Objects.isNull(workOrderFile)) {
            workOrderFile.setDeleteFlag(DataValidity.INVALID.getState());
            workOrderFileRepository.save(workOrderFile);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, "删除成功");
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "未找到文件信息");
        }
        return result;
    }
    public WorkOrderFile save(WorkOrderFileDomain domain){
        WorkOrderFile workOrderFile = new WorkOrderFile();
        BeanUtils.copyProperties(domain,workOrderFile);
        workOrderFile.setId(UUID.randomUUID().toString());
        workOrderFile.setDeleteFlag(DataValidity.VALID.getState());
        return workOrderFileRepository.save(workOrderFile);
    }
}
